﻿using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Text;
using TTEditor;
using TTEditor.Project;
using TTEngine;

namespace FakeUnity.Editor
{
    class WindowProperty : IWindow
    {
        public WindowProperty()
        {
        }
        public void OnClose()
        {
        }

        public void OnGUI()
        {
            var allselect = TTEditor.Selector.GetSelected(0);
            if (allselect == null || allselect.Count == 0)
            {
                ImGui.Text("没有选中任何物体");
            }
            else if (allselect.Count > 1)
            {
                ImGui.Text("选中 " + allselect.Count + " 个物体。");
                for (var i = 0; i < allselect.Count; i++)
                {
                    var obj = allselect[i];
                    ImGui.Text("name=" + obj.Name + " type=" + obj.GetType().Name);
                }
            }
            else
            {
                ImGui.Text("选中单个物体,可以编辑了");
                var obj = allselect[0];
                ImGui.Text("name=" + obj.Name + " type=" + obj.GetType().Name);
                if (obj is File)
                {
                    File f = obj as File;
                    OnGUI_File(f);
                }
                if (obj is Path)
                {
                    Path p = obj as Path;
                    ImGui.Text("选中路径" + p.Name);
                }
            }
        }
        void OnGUI_File(File f)
        {
            var converter = TTEditor.Resource.ConverterMgr.GetConverterByFileName(f.Name);
            if (converter == null)
            {
                ImGui.Text("没有对应的Converter");
            }
            else
            {
                ImGui.Text("converter:" + converter.GetType().Name);
                if (converter.HaveGUIExt)
                {
                    converter.OnGUI();//假入导入程序自带GUI扩展则执行它
                }
                else
                {
                    //Convert的自动化实现，这个和将来其它的应该可以复用同一份代码
                    TTEngine.Editor.ReflectionGUI.OnGUI(converter);
                }
                if (ImGui.Button("Convert"))
                {
                    try
                    {
                        TTEditor.Resource.ConverterMgr.Convert(converter, f.fullname, f.fullname + ".meta", true);
                    }
                    catch (Exception err)
                    {
                        //通过异常把Log信息带出来
                        Editor.logger.Error(err.Message);
                    }
                }
            }
            ImGui.NewLine();
            var assetname =Editor.curProject.GetAssetPath(f);
            IAsset asset = TTEngine.AssetMgr.GetAsset(assetname);
            if (asset == null)
            {
                ImGui.Text("asset=null");
            }
            else
            {
                TTEditor.Resource.ViewerMgr.OnGUI(asset);
            }
            //
        }

        public void OnUpdate()
        {
        }

        public void Show()
        {
        }
    }
}
